package com.hr.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.common.retrun.R;
import com.hr.system.entity.SysRole;
import com.hr.system.entity.param.RoleMenuUpdate;
import com.hr.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/sys/role")
@CrossOrigin
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("list")
    public R getList(SysRole sysRole, PageInfo<SysRole> pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getSize());
        List<SysRole> list = sysRoleService.getList(sysRole);
        return R.ok(new PageInfo<>(list));
    }
    @PostMapping("add")
    public R add(@RequestBody SysRole sysRole){
        if (sysRoleService.add(sysRole)>0)
            return R.ok("ok");
        else return R.error("error");
    }
    @PostMapping("update")
    public R update(@RequestBody SysRole sysRole){
        if (sysRoleService.update(sysRole)>0)
            return R.ok("ok");
        else return R.error("error");
    }
    @PostMapping("delete")
    public R delete(@RequestBody Map<String,Integer> map){
        if (sysRoleService.delete(map.get("id"))>0)
            return R.ok("ok");
        else return R.error("error");
    }
    @PostMapping("updatemenu")
    public R updateMenu(@RequestBody RoleMenuUpdate update){
        if (sysRoleService.updateMenu(update)>0)
        return R.ok("ok");
        else return R.error("error");
    }

    @GetMapping("rolemenu")
    public R rolemenu(Integer rid){
        return sysRoleService.queryByRid(rid);
    }
}
